package com.example.kliven.because.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.kliven.because.R;
import com.example.kliven.because.Singleton.VolleySingleton;
import com.example.kliven.because.adapter.NearbyAdapter;
import com.example.kliven.because.bean.NearbyBean;
import com.example.kliven.because.intentbean.NetNearbyBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class NearbyActivity extends Activity implements AdapterView.OnItemClickListener {

    private ListView listView;
    private NearbyAdapter nearbyAdapter;
    private List<NearbyBean> nearbyBeans;
    private String url = "http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_discover_nearby&parameters=%7B%22_latpoint%22:%2238.883596%22,%22_longpoint%22:%22121.54424%22,%22_start%22:%220%22,%22_end%22:%2215%22%7D&memberId=289167";
    private ArrayList<ArrayList<String>> allUrl = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nearby);

        listView = (ListView) findViewById(R.id.lv_nearby);
        listView.setOnItemClickListener(this);
        VolleySingleton.getInstance();
        getNetInfo();

    }

    private void getNetInfo() {
        VolleySingleton.addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                nearbyBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetNearbyBean netNearbyBean = gson.fromJson(response, NetNearbyBean.class);

                for (int i = 0; i < netNearbyBean.getData().getShop_and_url_item().size(); i++) {
                    NearbyBean nearbyBean = new NearbyBean();
                    nearbyBean.setShop_name(netNearbyBean.getData().getShop_and_url_item().get(i).getShop_name());
                    String spend = netNearbyBean.getData().getShop_and_url_item().get(i).getArg_spend();
                    String showSpend = spend.substring(0,spend.indexOf("."));
                    nearbyBean.setArg_spend(showSpend);
                    String distance = netNearbyBean.getData().getShop_and_url_item().get(i).getDistance();
                    String showDistance = distance.substring(0,distance.indexOf("."));
                    nearbyBean.setDistance(showDistance);
                    nearbyBean.setSharecount(netNearbyBean.getData().getShop_and_url_item().get(i).getSharecount());

                    String img = netNearbyBean.getData().getShop_and_url_item().get(i).getShare_url_and_id();
                    String imgMain = "";
                    imgMain = img.substring(0,img.indexOf(","));
//                    if (img.indexOf(";") != -1) {
//                        ArrayList<String> singleAll = new ArrayList<>();
//                        getUrl(singleAll, img);
//                        allUrl.add(singleAll);
//                    }

                    nearbyBean.setShare_url_and_id(imgMain);
                    nearbyBeans.add(nearbyBean);
                }
                nearbyAdapter = new NearbyAdapter(NearbyActivity.this, nearbyBeans);
                listView.setAdapter(nearbyAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }



    public void getUrl(ArrayList<String> data, String str) {
        int a = str.indexOf(",");
        if (str.indexOf(";") == -1) {
            return;
        }
        String url = str.substring(0, a);
        data.add(url);
        System.out.println(allUrl.toString());
        try {
            getUrl(data, str.substring(a + 7, str.length()));
        } catch (Exception e) {

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent nearbyIntent = new Intent(NearbyActivity.this,NearbyDetailActivity.class);
        Bundle setB = new Bundle();
        setB.putString("p", position + "");
        nearbyIntent.putExtra("nearbyBundle",setB);
        startActivity(nearbyIntent);
        Toast.makeText(this, "点击了第" + position, Toast.LENGTH_SHORT).show();
    }
}
